﻿var productRequestGrid = productRequestGrid || {};

productRequestGrid = function () {
    var gridInEditMode = false;
    var addcompanyDropDownChanged = false;
    var productRequestDataSource = new kendo.data.DataSource({
        transport: {
            read: {
                url: "/ProductRequestGrid/GetCompanyProject",
                type: "get",
               cache: false,
                dataType: "json"
            },

            create: {
                url: "/ProductRequestGrid/CreateProductRequest",
                type: 'POST',
                dataType: "json",
                contentType: "application/JSON"

            },
            update: {
                url: "/ProductRequestGrid/UpdateAddCompany",
                type: 'POST',
                dataType: "json",
                contentType: "application/JSON"
            },
            destroy: {
                url: "/ProductRequestGrid/DeleteAddCompany",
                type: "POST",
                dataType: "json",
                contentType: "application/JSON"
            },
            parameterMap: function (options) {
                return kendo.stringify(options); // kendo.stringify serializes to JSON string
            }

        },

        pageSize: 10,
        schema: {
            model: {
                id: "companyClientId",
                fields: {
                    companyClientId: {
                        editable: false, nullable: true,
                        type: "int"
                    },
                    prName: {
                        type:"string"
                    },
                    product:{
                        type:"string"
                    },
                    name: {
                        type: "string",
                        validation: { required: true }
                    },
                    paidCompany:
                        {
                            type: "string",
                            validation: { required: true }
                        },
                   
                    daysToFinish: {
                        type: "string",
                      //  validation: { required: true }
                    },
                    depositAmount: {
                        editable: true,
                        type: "int"
                    },

                    estimatedCost: {
                        type: "int",
                        validation: { required: true }
                    },
                    actualCost: {
                        type: "int",
                      //  validation: { required: true }
                    },
                    paidInFull:
                    {
                        type: "bool",
                        validation: { required: true }
                    },
                    taskScheduleComplete:
                  {
                      type: "bool",
                      validation: { required: true }
                  },
                    description: {

                        type: "string",
                        validation: { required: true }
                    },
                                  
                    contractSentDate: {
                        type: "date",
                        validation: { required: true }
                    },
                    contractSignedDate: {
                        type: "Date",
                        validation: { required: true }
                    },

                    depositReceivedDate:
                        {
                            type: "Date",
                            editabl: true,
                        },
                    startDate: {
                        type: "date"
                    },
                    expectedDeliveryDate:
                        {
                           type:"date" 
                        },
                    deliveryDate: {
                        type: "date"
                        // validation: { required: true}
                    },
                    notes: {
                        type: "string",
                        validation: { required: true }
                    },
                  
                    goLiveDate: {
                        type: "date"
                        // validation: { required: true}
                    },

                    estimatedDate:
                        {
                            type: "date"
                        },


                    error: function (a) {
                        $('#addcompanyGrid').data("kendoGrid").cancelChanges();
                    },
                }
            }
        }
    });
      
    var products = new kendo.data.DataSource({
        transport: {
            read: {
                url: "/ProductRequestGrid/GetProducts/",
                type: "get",
                contentType: "application/json",
                dataType: "json"
            }
        }
    });
    var department = new kendo.data.DataSource({
        transport: {
            read: {
                url: "/ProductRequestGrid/GetDepartments/",
                type: "get",
                contentType: "application/json",
                dataType: "json"
            }
        }
    });
    var projectManager = new kendo.data.DataSource({
        transport: {
            read: {
                url: "/ProductRequestGrid/GetProjectManagers/",
                type: "get",
                contentType: "application/json",
                dataType: "json"
            }
        }
    });

    var leadDevelopers = new kendo.data.DataSource({
        transport: {
            read: {
                url: "/ProductRequestGrid/GetLeadDevelopers/",
                type: "get",
                contentType: "application/json",
                dataType: "json"
            }
        }
    });
    var paidBy = new kendo.data.DataSource({
        transport: {
            read: {
                url: "/ProductRequestGrid/PaidBy/",
                type: "get",
                contentType: "application/json",
                dataType: "json"
            }
        }
    }); 

    return {
        productRequestDataSource: productRequestDataSource,
       
        department: department,
        products: products,
        paidBy: paidBy,
        projectManager: projectManager,
        leadDevelopers:leadDevelopers,
        gridInEditMode: gridInEditMode


    }

}();

$(function () {

    function dateTimeEditor(container, options) {
        $('<input data-text-field="' + options.field + '" data-value-field="' + options.field + '" data-bind="value:' + options.field + '" data-format="' + options.format + '"/>')
        .appendTo(container)
        .kendoDateTimePicker({});
    }


    $("#productRequestGrid").kendoGrid({
        dataSource: productRequestGrid.productRequestDataSource,
        scrollable: true,
        height: "300px",
        toolbar: ["create"], //, "save", "cancel"],
        pageable: true,
        scrollable:true,
        height: 250,
        columns: [      
       { field: "companyClientId", title: "Company", filterable: false, hidden: true , editable:true,width:"8em"},
        { field: "name", title: "Project Name", filterable: false,width:"8em" },
        { field: "product", title: "Product", filterable: false, editor:productDropDownEditor,width:"8em" },
        { field: "description", title: "Department", hidden: true, editor: DepartmentDropDownEditor, filterable: false, width: "8em" },
        { field: "managername", title: "ProjectManager", hidden: false, editor: projectManagerDropDownEditor, filterable: false, width: "8em" },
        { field: "developername", title: "Lead Developer", filterable: false,editor:leadDeveloperDropDownEditor, width: "8em" },
        { field: "legalName", title: "InvoluedEmployees", filterable: false ,width:"8em"},        
        
         { field: "daysToFinish", title: "Days To Finish", width: "10em"},
       { field: "depositsAmount", title: "Deposit", hidden:true },
   
      { field: "estimatedCosts", title: "Estimated Cost", width: "10em" },
         { field: "actualsAmount", title: "Actual Cost", hidden: true },
      
              { field: "statusId", title: "Status", width: "8em" },



               {
                   field: "paidInFull", title: "Full Paid ?", template: '<input type="checkbox" #= paidInFull ? \'checked="checked"\' : "" # class="chkbx" onclick="return false;" />',
                   filterable: {
                       messages: {
                           isTrue: "Active", // sets the text for "isTrue" radio button
                           isFalse: "Inactive",
                       }
                   },
                   width: "7em"
               },

               {
                   field: "taskScheduleComplete", title: "Task Schedule Complete  ?", template: '<input type="checkbox" #= taskScheduleComplete ? \'checked="checked"\' : "" # class="chkbx" onclick="return false;" />',
                   filterable: {
                       messages: {
                           isTrue: "Active", // sets the text for "isTrue" radio button
                           isFalse: "Inactive",
                       }
                   },
                   width: "7em"
               },
                
                 { field: "paidCompany", title: "Paid By", editor: paidByDropDownEditor, width: "7em" },
                {
                    field: "contractSentDate", title: "Contract Sent",hidden:true, filterable: false, format: "{0:MM/dd/yyyy HH:mm tt}",
                    editor: dateTimeEditor, 
                    filterable: {
                        ui: "datetimepicker"
                    }
                },
                  {
                      field: "contractSignedDate", title: "Contract Signed", hidden: true, filterable: false, format: "{0:MM/dd/yyyy HH:mm tt}",
                      editor: dateTimeEditor,
                      filterable: {
                          ui: "datetimepicker"
                      }
                  },
                     {
                         field: "depositReceivedDate", title: "Deposit Received", hidden: true, filterable: false, format: "{0:MM/dd/yyyy HH:mm tt}",
                         editor: dateTimeEditor,
                         filterable: {
                             ui: "datetimepicker"
                         }
                     },
                        {
                            field: "startDate", title: "Start Date", filterable: false, format: "{0:MM/dd/yyyy HH:mm tt}",
                            editor: dateTimeEditor, width: "8em",
                            filterable: {
                                ui: "datetimepicker"
                            }
                        },
                           {
                               field: "expectedDeliveryDate", title: "Expected Delivery", filterable: false, format: "{0:MM/dd/yyyy HH:mm tt}",
                               editor: dateTimeEditor, width: "8em",
                               filterable: {
                                   ui: "datetimepicker"
                               }
                           },

                            {
                                field: "deliveryDate", title: "Delivery", hidden:true, filterable: false, format: "{0:MM/dd/yyyy HH:mm tt}",
                                editor: dateTimeEditor,
                                filterable: {
                                    ui: "datetimepicker"
                                }
                            },

                             {
                                 field: "goLiveDate", title: "Go Live", hidden: true,editable:true, filterable: false, format: "{0:MM/dd/yyyy HH:mm tt}",
                                 editor: dateTimeEditor,
                                 filterable: {
                                     ui: "datetimepicker"
                                 }
                             },

                             {
                                 field: "estimatedDate", title: "Estimated Date", hidden: true,editable:true, filterable: false, format: "{0:MM/dd/yyyy HH:mm tt}",
                                 editor: dateTimeEditor, width: "8em",
                                 filterable: {
                                     ui: "datetimepicker"
                                 }
                             },
             { field: "notes", title: "Notes", width: "7em" },

      {
          title: "&nbsp;",
          width: "10em",
         // command: ["edit", "destroy"]
          command: [

              "edit",
              {
                  name: "Delete",


                  click: function (e) {
                     
                      var tr = $(e.target).closest("tr"); //get the row for deletion
                      var dataItem = this.dataItem(tr); //get the row data so it can be referred later
                      //see what is coming in dataItem
                      //if (dataItem.QuestionAnswers.length > 0) {
                      //    $("#delValidation").addClass('in')
                      //}
                      //else {

                          deleteTopicConfirmation(e, this);
                   //   }
                      return false;
                  }
              }
          ]
      }
        ],
        filterable: {
            extra: false,
            operators: {
                string: {
                    eq: "Is equal to",
                    neq: "Is not equal to"
                }
            }
        },

        editable: "popup",
        sortable: {
            mode: "single",
            allowUnsort: false
        },
        selectable: true,
        edit: function () {
            productRequestGrid.gridInEditMode = true;
        },
        cancel: function () {
            productRequestGrid.gridInEditMode = false;
        }
    });

    productRequestGrid.productRequestDataSource.bind("requestEnd", showSuccessMessage);


    function showSuccessMessage(e) {
        if (e.type === "update" || e.type === "create") {
            productRequestGrid.gridInEditMode = false;
            $("#saveConfirm").addClass('in');
            e.sender.read();
        }
    }


    $(".close").click(function () {
        $(this).parent().removeClass('in');
    });


    function deleteTopicConfirmation(e, widget) {
        var grid = $('#productRequestGrid').data("kendoGrid");
        deleteConfirmation(e, widget, grid);
    }

    function deleteConfirmation(e, widget, grid) {

        var currentRow = $(e.target).closest("tr"); //get the row for deletion

        var currentRowData = widget.dataItem(currentRow); //get the row data so it can be  referred later

        var kendoWindow = $("<div />").kendoWindow({

            title: "Confirmation",
            resizable: true,
            modal: true,
            width: "400px"
        });

        kendoWindow.data("kendoWindow")

            .content($("#delete-confirmation").html())

            .center().open();

        kendoWindow

            .find(".delete-confirm,.delete-cancel")
            .click(function () {
                if ($(this).hasClass("delete-confirm")) {

                    grid.dataSource.remove(currentRowData);

                    grid.dataSource.sync();

                }

                $(this).closest("[data-role=window]").kendoWindow("close");

            });

    }

    function productDropDownEditor(container, options) {
        $('<input id="productId" data-text-field="product" data-value-field="product"  data-bind="value:' + options.field + '"/>').appendTo(container).kendoDropDownList({
            autoBind: false,
            dataValueField: "product",
            dataTextField: "product",
            dataSource: productRequestGrid.products

        });
    }

    function DepartmentDropDownEditor(container, options) {
        $('<input id="departmentId" data-text-field="description" data-value-field="description"  data-bind="value:' + options.field + '"/>').appendTo(container).kendoDropDownList({
            autoBind: false,
            dataValueField: "description",
            dataTextField: "description",
            dataSource: productRequestGrid.department

        });
    }
    function projectManagerDropDownEditor(container, options) {
        $('<input id="ManagerId" data-text-field="managername" data-value-field="managername"  data-bind="value:' + options.field + '"/>').appendTo(container).kendoDropDownList({
            autoBind: false,
            dataValueField: "managername",
            dataTextField: "managername",
            dataSource: productRequestGrid.projectManager

        });
    }


    function leadDeveloperDropDownEditor(container, options) {
        $('<input id="leaddeveloper" data-text-field="developername" data-value-field="developername"  data-bind="value:' + options.field + '"/>').appendTo(container).kendoDropDownList({
            autoBind: false,
            dataValueField: "developername",
            dataTextField: "developername",
            dataSource: productRequestGrid.leadDevelopers

        });
    }
    function paidByDropDownEditor(container, options) {
        $('<input id="paidById" data-text-field="paidCompany" data-value-field="paidCompany"  data-bind="value:' + options.field + '"/>').appendTo(container).kendoDropDownList({
            autoBind: false,
            dataValueField: "paidCompany",
            dataTextField: "paidCompany",
            dataSource: productRequestGrid.paidBy

        });
    }

    


});


